/**
 * Created by iallai on 2/1/2018.
 */

import axios from 'axios'
import {CONFIG} from '@/config/env'

let API=CONFIG.PROJECT.API;

/**
 * 查询店铺列表
 * @param {Object} query - 店铺查询参数
 * @param {string} query.size
 * @param {string} query.page
 * @param {string} query.keyword
 * @param {string} config.child 子资源名
 */

const query = (query = {}, config) => new Promise((resolve, reject) => {

  let childname = config && config.child ? `/${config.child}` : '';

  axios.get(`${API.URL}/${API.APPNAME}/shops${childname}`, {
    params: query,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }
  })
    .catch(function(err){
      reject("网络错误");
    });


});


const get = (id, config) => new Promise((resolve, reject) => {
  let childname = config && config.child ? `/${config.child}` : '';

  axios.get(`${API.URL}/${API.APPNAME}/shops${childname}/${id}`).then(function (res) {
    if (API.isOK(res.data)) {
      resolve(res.data);
    }
  })
    .catch(function(err){
      reject("网络错误");
    });



});

const audit = (id, code) => new Promise((resolve, reject) => {


  axios({
    method: 'post',
    url: `${API.URL}/${API.APPNAME}/shops/applies/${id}/audit`,
    data: {
      approved: code
      // shop: shop
    },

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
  }).catch(function(err){
    reject("网络错误");
  });

});


const edit = (data = {}) => new Promise((resolve, reject) => {


  axios({
    method: 'post',
    url: `${API.URL}/${API.APPNAME}/shops/${data.id}`,
    data: data,

    }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
    }).catch(function(err){
    reject(err);
    });

});

const del = (id) => new Promise((resolve, reject) => {


  axios({
    method: 'delete',
    url: `${API.URL}/${API.APPNAME}/shops/${id}`
  }).then(function (res) {
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
  }).catch(function (err) {
    reject(err);
  });

});


export {get, query, edit, del, audit}

